package com.fiberhome.nas.core.database.entity

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.fiberhome.nas.core.model.entity.TaskStatus
import kotlinx.serialization.json.Json

@Entity(tableName = "upload_tasks")
data class UploadTaskEntity(
    @PrimaryKey val id: String,
    val userId: String,
    val fileName: String,
    val fileSize: Long,
    val filePath: String,
    val fileType: String,
    @TypeConverters(UploadTaskStatusConverter::class)
    val status: TaskStatus = TaskStatus.Waiting,
    val uploadPath: String,
    val uploadedSize: Long,
    val totalSize: Long,
    val currentChunk: Int,
    val totalChunk: Int,
    val labelTip: String,   // 显示网速/失败原因/上传位置
    val favorite: Boolean?,
    val duration: Long?,
    val albumId: String?,
    val mediaId: String = "0",   // 媒体文件ID
)

class UploadTaskStatusConverter {

    private val json = Json { ignoreUnknownKeys = true }

    @TypeConverter
    fun fromStatus(value: TaskStatus): String {
        return json.encodeToString(value)
    }

    @TypeConverter
    fun toStatus(value: String): TaskStatus {
        return json.decodeFromString(value)
    }
}